﻿using System;
using System.Text;
using System.Security.Cryptography;
using System.Text.RegularExpressions;

namespace DatabaseAccess
{
    public class Utilities
    {
     

        //加速字符串产生
        private static Char[] codes = {   /*大写字母0~25*/
                                                'A','B','C','D','E','F','G','H','I','J','K','L','M','N',
                                                'O','P','Q','R','S','T','U','V','W','X','Y','Z',
                                                /*小写字母26~51*/
                                                'a','b','c','d','e','f','g','h','i','j','k','l','m','n',
                                                'o','p','q','r','s','t','u','v','w','x','y','z',
                                                /*数字52~61*/
                                                '0','1','2','3','4','5','6','7','8','9',
                                                /*特殊字符62~94*/
                                                '!','"','#','$','%','&','\'','(',')','*','+',',','-','.','/',
                                                ':',';','<','=','>','?','@','[','\\',']','^','_','`','{','|','}','~',''
                                            };

        //减少随机冲突
        private static Random random = new Random();

        #region 随机字符串
        public static string RandomString(int length)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++)
            {
                sb.Append((char)( codes[random.Next(62)]));
            }
            return sb.ToString();
        }

        #endregion

        //get salt
        #region 获得盐的值

        public static String GetSalt()
        {
            return RandomString(16);
        }

        #endregion

        //get md5 value
        //获得加盐密码的md5值 调用GetMD5(password+salt)
        #region 获得md5值

        public static string GetMD5(string msg)
        {
            StringBuilder sb = new StringBuilder();

            using (MD5 md5 = MD5.Create())
            {
                byte[] buffer = Encoding.UTF8.GetBytes(msg);
                byte[] bytes = md5.ComputeHash(buffer);

                foreach (byte item in bytes)
                {
                    sb.Append(item.ToString("x2"));
                }
            }

            return sb.ToString();
        }

        #endregion

        //判断字符串合法性，是否含非法字符（如各种标点符号等）
        //信息量较大 不用正则表达式
        #region 判断字符串合法性
        //CJK字符串判断
        public static bool IsCJKChar(String str)
        {
            // 合法的字符为：
            // 字母,数字,字符： a-z      A-Z     0-9      -   .   @    _;
            // 对应的ASCII码：  97-123   65-90   48-57   45  46  64   95;
            // CJK字符：        4E00-9FBF：CJK 统一表意符号 (CJK Unified Ideographs)
            string s = str.ToLower();
            foreach (char c in s)
            {
                if (c >= 'a' && c <= 'z') continue;
                if (c >= '0' && c <= '9') continue;
                if ('.' == c || '-' == c || '_' == c || '@' == c) continue;
                if (c >= '\u4E00' && c <= '\u9FBF') continue;
                return false;
            }
            return true;
        }

        //ANSI字符串判断
        public static bool IsANSIChar(String str)
        {
            // 合法的字符为;
            // 字母,数字,字符:  a-z      A-Z     0-9      -   .   @    _;
            // 对应的ASCII码：  97-123   65-90   48-57   45  46  64   95;
            string s = str.ToLower();
            foreach (char c in s)
            {
                if (c >= 'a' && c <= 'z') continue;
                if (c >= '0' && c <= '9') continue;
                if ('.' == c || '-' == c || '_' == c || '@' == c) continue;
                return false;
            }
            return true;
        }
        #endregion

        //判断邮箱输入合法性
        #region 判断邮箱输入
        public static bool isEmail(String email)
        {
            return Regex.IsMatch(email, @"^\s*([A-Za-z0-9_-]+(\.\w+)*@([\w-]+\.)+\w{2,10})\s*$"); 
        }
        #endregion
    }
}
